package handler

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"strconv"
	"time"
	"user_srv/global"
	"user_srv/internal/model/dao"
	"user_srv/internal/service"
	user "user_srv/proto"
)

type UserServer struct {
	user.UnimplementedUserServer
}

func (u *UserServer) Register(ctx context.Context, req *user.RegisterRequest) (resp *user.RegisterResponse, err error) {

	exists, _ := service.GetUserIsExists(req.Mobile)
	if exists.ID != 0 {
		return nil, fmt.Errorf("用户已注册")
	}
	tx := global.DB.Begin()
	Req := dao.User{
		Mobile:   req.Mobile,
		Password: req.Password,
	}
	err = tx.Create(&Req).Error
	if err != nil {
		tx.Rollback()
		return nil, fmt.Errorf(err.Error())
	}
	coupon := dao.Coupon{
		ID:         strconv.Itoa(int(uuid.New().ID())),
		UserID:     0,
		Types:      0, //代表默认优惠卷
		CouponName: "满100减5优惠券",
	}
	err = tx.Create(&coupon).Error
	if err != nil {
		tx.Rollback()
		return nil, fmt.Errorf(err.Error())
	}
	return &user.RegisterResponse{
		Pong: "注册成功",
	}, nil
}
func (u *UserServer) Login(ctx context.Context, req *user.LoginRequest) (resp *user.LoginResponse, err error) {

	exists, _ := service.GetUserIsExists(req.Mobile)
	if err != nil {
		return nil, fmt.Errorf("用户不存在")
	}
	if exists.Password != req.Password {
		return nil, fmt.Errorf("密码错误")
	}
	key := fmt.Sprintf("user%d_login", exists.ID)
	global.Rdb.Set(ctx, key, exists.ID, time.Minute*60)

	return &user.LoginResponse{
		Id: int32(exists.ID),
	}, nil
}
func (u *UserServer) CreateCourseInfo(ctx context.Context, req *user.CreateCourseInfoRequest) (resp *user.CreateCourseInfoResponse, err error) {

	key := fmt.Sprintf("user%d_login", req.UserId)
	err = global.Rdb.Get(ctx, key).Err()
	if err != nil {
		return nil, fmt.Errorf("用户未登录")
	}
	ALlCourseInfo := service.GetALlCourseInfo()

	study := dao.Study{
		CourseID:   int(ALlCourseInfo.ID),
		CourseName: ALlCourseInfo.Name,
		Price:      ALlCourseInfo.Price,
	}
	err = global.DB.Create(&study).Error
	if err != nil {
		return nil, fmt.Errorf("加入学习计划失败")
	}

	return &user.CreateCourseInfoResponse{Pong: "加入学习计划成功"}, nil
}
func (u *UserServer) DelStudy(ctx context.Context, req *user.DelStudyRequest) (resp *user.DelStudyResponse, err error) {

	key := fmt.Sprintf("user%d_login", req.UserId)
	err = global.Rdb.Get(ctx, key).Err()
	if err != nil {
		return nil, fmt.Errorf("用户未登录")
	}
	studyPlan := service.GetStudyPlan(int(req.StudyId))

	plan := service.GetDelStudyPlan(int(studyPlan.ID))
	if !plan {
		return nil, fmt.Errorf("删除失败")
	}
	return &user.DelStudyResponse{Pong: "删除目标成功"}, nil
}
func (u *UserServer) CreateOrder(ctx context.Context, req *user.CreateOrderRequest) (resp *user.CreateOrderResponse, err error) {

	info := service.GetCourseInfo(int(req.CourseId))

	order := dao.Orders{
		Sn:       uuid.NewString(),
		CourseId: int(info.ID),
		UserId:   int(req.UserId),
		Status:   0,
		Price:    info.Price,
	}
	err = global.DB.Create(&order).Error
	if err != nil {
		return nil, fmt.Errorf("订单创建失败")
	}

	return &user.CreateOrderResponse{Pong: "订单创建成功"}, nil
}

func (u *UserServer) Pay(ctx context.Context, req *user.PayRequest) (resp *user.PayResponse, err error) {

	pay := dao.Pay{
		Price:    float64(req.Price),
		Discount: float64(req.Discount),
	}
	global.DB.Create(&pay)
	return &user.PayResponse{Pong: "支付成功"}, nil
}
